package com.github.yeecode.mybatisdemo;

import org.apache.ibatis.executor.BatchExecutor;
import org.apache.ibatis.executor.ReuseExecutor;
import org.apache.ibatis.executor.SimpleExecutor;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.*;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExecutorTest {

    private Configuration configuration;
    private Connection connection;
    private JdbcTransaction jdbcTransaction;

    /* 表中数据
    name : 易哥 ;  email : yeecode@sample.com
    name : 杰克 ;  email : jack@sample.com
    name : 王小壮 ;  email : wxiaozhuang@sample.com
    name : 李二壮 ;  email : lerzhuang@sample.com
    */
    @Before
    public void init() throws Exception {
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory builder = factoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        configuration = builder.getConfiguration();
        connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/yeecode?serverTimezone=UTC",
                "root","123456");
        jdbcTransaction = new JdbcTransaction(connection);
    }

    //简单执行器
    @Test
    public void simpleTest() throws SQLException {
        SimpleExecutor executor = new SimpleExecutor(configuration,jdbcTransaction);
        MappedStatement ms = configuration.getMappedStatement("com.github.yeecode.mybatisdemo.UserMapper.getByName");
        List<User> list = executor.doQuery(ms,"易哥", RowBounds.DEFAULT,
                SimpleExecutor.NO_RESULT_HANDLER,ms.getBoundSql("易哥"));
        List<User> list2 = executor.doQuery(ms,"杰克", RowBounds.DEFAULT,
                SimpleExecutor.NO_RESULT_HANDLER,ms.getBoundSql("杰克 "));

        System.out.println(list.get(0).toString());
    }

    //可重用执行器，参数不一样，也只需要一次Preparing:
    @Test
    public void reuseTest() throws SQLException {
        ReuseExecutor executor = new ReuseExecutor(configuration,jdbcTransaction);
        MappedStatement ms = configuration.getMappedStatement("com.github.yeecode.mybatisdemo.UserMapper.getById");
        List<User> list = executor.doQuery(ms,1l, RowBounds.DEFAULT,
                SimpleExecutor.NO_RESULT_HANDLER,ms.getBoundSql(1l));
        List<User> list2 = executor.doQuery(ms,2l, RowBounds.DEFAULT,
                SimpleExecutor.NO_RESULT_HANDLER,ms.getBoundSql(2l));

        System.out.println(list.get(0).toString());
        System.out.println(list2.get(0).toString());
    }

    // p批处理，必须手动刷新，才能提交
    // 同ReuseExecutor，只需要一次Preparing:
    @Test
    public void batchUpdate() throws SQLException {
        BatchExecutor executor = new BatchExecutor(configuration,jdbcTransaction);
        MappedStatement ms = configuration.getMappedStatement(
                "com.github.yeecode.mybatisdemo.UserMapper.updateById");

        PreparedStatement preparedStatement = connection.prepareStatement("select * from user");

        Map map = new HashMap<>();
        map.put("id",2l );
        map.put("name","wentf");
        int update = executor.doUpdate(ms, map);
        int update2  = executor.doUpdate(ms, map);
        executor.flushStatements();
        System.out.println(update);
        System.out.println();


        preparedStatement.executeQuery();
    }
}
